# encoding:utf8
import tensorflow as tf
import logging
import numpy as np

logging.basicConfig(level=logging.DEBUG)
LOGGING = logging.getLogger("tfrecord_reader")


def main():
    reader = tf.TFRecordReader()
    file_queue = tf.train.string_input_producer(['E:/mnist.tfrecords'])
    _, serialized_example = reader.read(file_queue)
    features = tf.parse_single_example(serialized_example, features={
        'image_raw': tf.FixedLenFeature([], tf.string),
        'pixels': tf.FixedLenFeature([], tf.int64),
        'label': tf.FixedLenFeature([], tf.int64)
    })
    image = tf.decode_raw(features['image_raw'], tf.uint8)
    pixels = tf.cast(features['pixels'], tf.int32)
    label = tf.cast(features['label'], tf.int32)

    sess = tf.Session()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    for i in range(10):
        LOGGING.info(sess.run([image, label, pixels]))


if __name__ == '__main__':
    main()
